<?php
session_start();

include("Functions.php");
include("DB.class.php");
include("Validator.class.php");
$DB = new DB();
$Val = new Validator();


$command_id = $_REQUEST['command_id'];$er = $_REQUEST['error'];	
if(isset($command_id)&&$command_id ==7){;session_unset();unset($command_id);}		
if(!isset($command_id) || $command_id ==4){

include("Design.php");



Top();
?>

<!-- Formulář - zobrazí se pouze když PHP proměnná $showForm==true -->



<form action="index.php?command_id=1" method="post">
		
	<br/><br/>
	<label for="name"> Name</label>
	<input type="text" id="name" name="name" value="<?php echo $_POST['name'];?>">
    
	<br/><br/>
	<label for="pass"> Password</label>
	<input type="password" id="pass" name="password" value="<?php echo $_POST['password'];?>">

	<input type="submit" name="Login" value="login">
</form>
<?php

//echo strlen(generateHash($passw));
if($command_id ==4){unset($_POST);echo "</br><div style='background-color: darkred'>Wrong login name or password</div>" ;}


ShowFooter();
}
else{
if($command_id==1){
	
$login = $_REQUEST['name'];
$password = $_REQUEST['password'];
if($DB->CheckLogin($login,$password)){
header('Location: NovyTest.php?cmd=1');
}
else {
header('Location: index.php?command_id=4');
}
}
if($command_id==2||$command_id==5||$command_id==6){
include("Design.php");
Top();
?>

<!-- Formulář - zobrazí se pouze když PHP proměnná $showForm==true -->



<form action="index.php?command_id=3" method="post">
		
	<br/><br/>
	<label for="name"> Name</label>
	<input type="text" id="name" name="name" value="<?php echo $_POST['name'];?>">
    
	<br/><br/>
	<label for="pass"> Password</label>
	<input type="password" id="pass" name="password" value="<?php echo $_POST['password'];?>">

	<br/><br/>
	<label for="email"> e-mail</label>
	<input type="text" id="email" name="email" value="<?php echo $_POST['email'];?>">
	
	<input type="submit" name="Sign Up!" value="signup">
</form>

<?php
if($command_id ==5){unset($_POST);echo "</br><div style='background-color: darkred'>Chybně vyplněné údaje: ".$er."</div>" ;}
if($command_id ==6){unset($_POST);echo "</br><div style='background-color: darkred'>Login name není unikátní</div>" ;}
}
if($command_id==3){
	
$login = $_REQUEST['name'];
$password = $_REQUEST['password'];
$email = $_REQUEST['email'];
$v=$Val->IsValid($login, $password, $email);
if($v==1){
	$rs=$DB->Register($login,$password,$email);
	
	if($rs){
	$DB->CheckLogin($login,$password);
	header('Location: NovyTest.php?cmd=2');
}
	else{
	header('Location: index.php?command_id=6');}
}
else{
header('Location: index.php?command_id=5&error='.$v.'');

}

}


}

?>


</body>
</html>